/*
 * Dialog ini muncul tiap kali pemain harus membayar sewa lokasi pariwisata ke pemain lain
 * 
 */

package controller.permainan;
import helper.*;
import model.players.*;
import javax.swing.JOptionPane;

public class Bayar_dialog extends javax.swing.JDialog {

    public Permainan2 game;
    public Player player;
    public Player owner;

    /** Creates new form Bayar_dialog */
    public Bayar_dialog(java.awt.Frame parent, boolean modal, Permainan2 _game) {
        super(parent, modal);
        game = _game;
        player = game.G.current_plyr();
        owner = game.G.current_kotak().getOwner();
        initComponents();
        this.setLocationRelativeTo(null);
        playericon_label.setIcon(player.getLargeImage());
        if(owner != null)
            ownericon_label.setIcon(owner.getLargeImage());
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pay_button = new javax.swing.JButton();
        paydesc_label = new javax.swing.JLabel();
        paylater_button = new javax.swing.JButton();
        playericon_label = new javax.swing.JLabel();
        ownericon_label = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        pay_button.setText("Pay Now");
        pay_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pay_buttonActionPerformed(evt);
            }
        });

        paydesc_label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        paydesc_label.setText("jLabel1");

        paylater_button.setText("Pay Later");
        paylater_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                paylater_buttonActionPerformed(evt);
            }
        });

        playericon_label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        ownericon_label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addComponent(pay_button, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 248, Short.MAX_VALUE)
                .addComponent(paylater_button, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(34, 34, 34))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(paydesc_label, javax.swing.GroupLayout.DEFAULT_SIZE, 503, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addComponent(playericon_label, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 94, Short.MAX_VALUE)
                .addComponent(ownericon_label, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(59, 59, 59))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(ownericon_label, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE)
                    .addComponent(playericon_label, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(paydesc_label, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(paylater_button, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pay_button, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        getAccessibleContext().setAccessibleParent(this);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void pay_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pay_buttonActionPerformed
        //KONDISI : LOKASI PARIWISATA DAN PUNYA SATU KOMPLEKS
        if ( (game.G.current_kotak().getTipe() == 2) && (SearchEngine.DoYouHaveThisComplex(game.G.B, game.G.current_kotak().getPosisi(), game.G.current_kotak().getOwner())))
        {
            //KONDISI : CURRENT PLAYER CUKUP UANG, HARGA SEWA 2 KALI LIPAT
            if (game.G.current_plyr().bayar(2*game.G.current_kotak().getHargasewa()))
            {
                game.G.current_kotak().getOwner().addUang(2*game.G.current_kotak().getHargasewa());
                game.HistoryWrite("\t"+game.G.current_plyr().getNama()+" has pay rent Rp. "+2*game.G.current_kotak().getHargasewa()+" to "+game.G.current_kotak().getOwner().getNama());
                game.G.current_plyr().sudah_bayar = true;
            }
            //KONDISI : CURRENT PLAYER TIDAK PUNYA CUKUP UANG
            else
            {
                JOptionPane.showMessageDialog(rootPane, "NFS. Not sufficient fund. Sell something dong!", "MonopoSims", JOptionPane.ERROR_MESSAGE);
                game.NotifWrite("You dont have sufficient fund!");
            }
        }
        //KONDISI : LOKASI PARIWISATA DAN BELUM PUNYA SATU KOMPLEKS
        else if ( (game.G.current_kotak().getTipe() == 2) && (!SearchEngine.DoYouHaveThisComplex(game.G.B, game.G.current_kotak().getPosisi(), game.G.current_kotak().getOwner())))
        {
            //KONDISI : CURRENT PLAYER CUKUP UANG
            if (game.G.current_plyr().bayar(game.G.current_kotak().getHargasewa()))
            {
                game.G.current_kotak().getOwner().addUang(game.G.current_kotak().getHargasewa());
                game.HistoryWrite("\t"+game.G.current_plyr().getNama()+" has pay rent Rp. "+game.G.current_kotak().getHargasewa()+" to "+game.G.current_kotak().getOwner().getNama());
                game.G.current_plyr().sudah_bayar = true;
            }
            //KONDISI : CURRENT PLAYER TIDAK PUNYA CUKUP UANG
            else
            {
                JOptionPane.showMessageDialog(rootPane, "NFS. Not sufficient fund. Sell something dong!", "MonopoSims", JOptionPane.ERROR_MESSAGE);
                game.NotifWrite("You dont have sufficient fund!");
            }
        }
        //KONDISI : AIRPORT
        else if (game.G.current_kotak().getTipe() == 4)
        {
            int jum_airport = helper.SearchEngine.howManyAirportsDoYouHave(game.G.current_kotak().getOwner());
            //KONDISI : CURRENT PLAYER CUKUP UANG, KALI DENGAN BANYAK AIRPORT YANG DIA PUNYA
            if (game.G.current_plyr().bayar(jum_airport*game.G.current_kotak().getHargasewa()))
            {
                game.G.current_kotak().getOwner().addUang(jum_airport*game.G.current_kotak().getHargasewa());
                game.HistoryWrite("\t"+game.G.current_plyr().getNama()+" has pay rent Rp. "+jum_airport*game.G.current_kotak().getHargasewa()+" to "+game.G.current_kotak().getOwner().getNama());
                game.G.current_plyr().sudah_bayar = true;
            }
            //KONDISI : CURRENT PLAYER TIDAK PUNYA CUKUP UANG
            else
            {
                JOptionPane.showMessageDialog(rootPane, "NFS. Not sufficient fund. Sell something dong!", "MonopoSims", JOptionPane.ERROR_MESSAGE);
                game.NotifWrite("You dont have sufficient fund!");
            }
        }
        game.ShowPlyrAttr(game.G.urutan_current_plyr);
        this.dispose();
    }//GEN-LAST:event_pay_buttonActionPerformed


    private void paylater_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_paylater_buttonActionPerformed
              this.dispose();
    }//GEN-LAST:event_paylater_buttonActionPerformed

    @SuppressWarnings("static-access")
    public void setPaydesc(String S)
    {
        paydesc_label.setText(S);
        paydesc_label.setHorizontalTextPosition(paydesc_label.CENTER);
    }

    public static void main(String args[]) {
        final Permainan2 game = new Permainan2();
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Bayar_dialog dialog = new Bayar_dialog(new javax.swing.JFrame(), true, game);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel ownericon_label;
    private javax.swing.JButton pay_button;
    private javax.swing.JLabel paydesc_label;
    private javax.swing.JButton paylater_button;
    private javax.swing.JLabel playericon_label;
    // End of variables declaration//GEN-END:variables

}
